using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;

namespace CityLampConfigForm
{
    public partial class Form1 : Form
    {
        private string strFilePath = "";
        private bool beNeedServer = false;
        private string strDomainName = "";
        private string strIPAddress = "";
        private UInt16 Port = 0;

        public Form1()
        {
            strFilePath = System.Windows.Forms.Application.StartupPath;
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.LoadConfig(strFilePath);
            if (beNeedServer)
            {
                this.radioButtonServer.Checked = true;
                this.radioButtonNoServer.Checked = false;
            }
            else
            {
                this.radioButtonServer.Checked = false;
                this.radioButtonNoServer.Checked = true;
            }
            this.textBoxDomainName.Text = this.strDomainName;
            this.textBoxPort.Text = this.Port.ToString();
            this.textBoxIP.Text = this.strIPAddress;
        }

        private void LoadConfig(string _strFilePath)
        {
            string strCfgFile = _strFilePath + "\\Config.ini";

            IniFiles cfg = new IniFiles(strCfgFile);

            beNeedServer = cfg.ReadBool("MODE", "NEEDSERVER", false);
            strDomainName = cfg.ReadString("SERVER", "DOMAINNAME", "");
            strIPAddress = cfg.ReadString("SERVER", "IP", "");
            Port = (UInt16)cfg.ReadInteger("SERVER", "PORT", 7788);
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void radioButtonNoServer_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButtonNoServer.Checked)
            {
                this.groupBox2.Enabled = false;
            } 
            else
            {
                this.groupBox2.Enabled = true;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {

            string strCfgFile = strFilePath + "\\Config.ini";
            IniFiles cfg = new IniFiles(strCfgFile);

            if (this.radioButtonNoServer.Checked)
            {
                cfg.WriteBool("MODE", "NEEDSERVER", false);
                this.Close();
            }
            else
            {
                cfg.WriteBool("MODE", "NEEDSERVER", true);
                cfg.WriteString("SERVER", "DOMAINNAME", this.textBoxDomainName.Text);
                cfg.WriteString("SERVER", "IP", this.textBoxIP.Text);
                cfg.WriteInteger("SERVER", "PORT", UInt16.Parse(this.textBoxPort.Text));
            }

            try
            {
                IPHostEntry server = Dns.GetHostByName(this.textBoxDomainName.Text);
                //IPEndPoint ServerIPEP = new IPEndPoint(server.AddressList[0], UInt16.Parse(this.textBoxPort.Text));
                //Socket m_ServerSocket = new Socket(ServerIPEP.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
                //m_ServerSocket.Connect(ServerIPEP);                
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
            this.Close();
        }
    }
}